package com.link.web.handler;


import com.link.base.excepiton.SystemException;
import com.link.base.response.Response;
import com.link.base.response.ResponseCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author ttao
 * @since 2024/9/29 20:09
 */
@RestControllerAdvice
public class GlobalWebExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Response handleValidationExceptions2(MethodArgumentNotValidException ex) {
        String errorMessage = ex.getBindingResult().getFieldError().getDefaultMessage();
        return Response.fail(ResponseCode.ILLEGAL_ARGUMENT,errorMessage);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(SystemException.class)
    public Response systemExceptionHandler(SystemException systemException) {
        Response result = new Response();
        result.setCode(systemException.getErrorCode().getCode());
        result.setMessage(systemException.getErrorCode().getMessage());
        result.setSuccess(false);
        return result;
    }

}
